package com.example.validatestudy.demos.dataStructures;

import java.util.*;

/**
 * @auther: 王振方
 * @date: 2024 05 19 09 39
 * @description:
 */
public class TreeTest {

    public static void main(String[] args) {
        TreeNode t = init();

        //前序遍历  3 2 1 5 4 6
        System.out.println("前序遍历");
        preOrder(t);

        //中序遍历  1 2 3 4 5 6
        System.out.println("中序遍历");
        middleOrder(t);

        //后序遍历  1 2 4 6 5 3
        System.out.println("后序遍历");
        afterOrder(t);

        //层次遍历
        System.out.println("层次遍历");
        levelOrder(t);

    }

    /**
     * 前序遍历
     */
    public static void preOrder(TreeNode node){
        if(node==null){
            return;
        }
        System.out.println(node.val);
        preOrder(node.left);
        preOrder(node.right);
    }

    /**
     * 中序遍历
     */
    public static void middleOrder(TreeNode node){
        if(node==null){
            return;
        }
        middleOrder(node.left);
        System.out.println(node.val);
        middleOrder(node.right);
    }

    /**
     * 后序遍历
     */
    public static void afterOrder(TreeNode node){
        if(node==null){
            return;
        }
        afterOrder(node.left);
        afterOrder(node.right);
        System.out.println(node.val);
    }


    /**
     * 广度遍历
     * @param treeNode
     * @return
     * queue.poll();  检索并删除第一个元素
     * queue.remove(); Removes and returns the first element from this list.
     * poll和remove都是获取第一个元素，区别是第一个元素为空时，poll返回null，remove报异常。
     *
     * queue.offer(treeNode);  添加指定元素作为此列表的尾部(最后一个元素)。
     * queue.add(treeNode);  添加指定元素作为此列表的尾部(最后一个元素)。
     * queue.peek();  Retrieves, but does not remove, the head (first element) of this list.
     */
    public static void levelOrder(TreeNode treeNode){
        Queue<TreeNode> queue = new LinkedList();
        queue.add(treeNode);
        while (!queue.isEmpty()){
            TreeNode node  =  queue.poll();
            System.out.println(node.val);
            if(node.left!=null){
                queue.add(node.left);
            }
            if(node.right!=null){
                queue.add(node.right);
            }
        }
    }

    /**
     * 初始化获取一个树
     *     3
     *   2   5
     * 1    4  6
     * @return
     */
    private static TreeNode init(){
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        TreeNode t4 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t6 = new TreeNode(6);
        t3.setLeft(t2);
        t2.setLeft(t1);
        t3.setRight(t5);
        t5.setRight(t6);
        t5.setLeft(t4);
        return t3;
    }


    private static class TreeNode{
        private Integer val;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(Integer val) {
            this.val = val;
        }

        public TreeNode(Integer val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }
    }

}
